<!-- 卡通图 -->
<template>
	<page-meta :root-font-size="getRootFontSize()"></page-meta>	
	<view class="">
		<view class="topBox">
			<view class="status_bar"></view>
			<uni-nav-bar
				style="width: 750rpx; height: 44px;"
				@clickRight="showSetUp"
				@clickLeft="goHome"
				color="#FDFDFD"
				:border="false"
				:shadow="false"
				background-color="#F1F1F1"
			>
				<view class="titleImg"><image style="width: 100%; height: 100%;" mode="scaleToFill" src="/static/icon/logo_ctw@3x.png"></image></view>
				<view slot="left">
					<view class="leftIconBox"><uni-icons type="arrowleft" color="#000" size="18"></uni-icons></view>
				</view>
				<view slot="right"><uni-icons type="bars" color="#7F7F7F" size="28"></uni-icons></view>
			</uni-nav-bar>
		</view>
		<view class="zhuti" :style="{ marginTop: topBar }">
			<view class="imageTopBox"><image style="width: 100%; height: 100%;" mode="scaleToFill" src="/static/icon/HowtoOrder/2x/pic_banner01@2x.png"></image></view>
			<view class="tabTopBox">
				<!-- {{ i18n.ctwfont1 }} -->
				<view class="tabTopBoxL"></view>
				<view class="tabTopBoxR" @tap="downLoad">
					<view class="tabTopBoxRImg"><image style="width: 13px; height: 13px;" mode="scaleToFill" src="/static/icon/HowtoOrder/2x/HowtoOrder2@2x.png"></image></view>
					<view class="tabTopBoxRText">
						<view style="color: #63646F;">DOWNLOAD</view>
						<view style="color: #41518B;">Handbook</view>
					</view>
				</view>
			</view>
			<!-- 内容 -->
			<view class="neironBox"><image style="width: 100%; height: 100%;" mode="scaleToFill" src="https://ctwcms.oss-ap-southeast-1.aliyuncs.com/web/ctw_markets/app_pic_long.png"></image></view>
		</view>
		<uni-popup ref="ImgPopup" type="center" :mask-click="false">
			<arprogress :percent="percent" :width="150" activeColor="#01AD50" inactiveColor="#ececec" :borderWidth="10">
				<text style="color: #ececec;">{{ percent }}%</text>
			</arprogress>
		</uni-popup>
		<!-- 设置 -->
		<uni-popup ref="SetUpPopup" type="center" :animation="false"><set-up-pages ref="showPopup" @closePopUp="closePopUp" /></uni-popup>
		<!-- 退出 -->
		<sign-out ref="signOutBox" />
		<!-- 底部菜单 -->
		<bottom-pages></bottom-pages>
	</view>
</template>

<script>
import setUpPages from '@/pages/setUpPages/index.vue';
import signOut from '@/pages/setUpPages/signOut.vue'; // 退出登录
import bottomPages from '@/pages/home/bottomPages.vue'; // 底部
import arprogress from '@/components/ar-circle-progress/ar-circle-progress.vue';
import baseFont from '@/utils/baseFont.js';

export default {
	extends: baseFont,
	data: function() {
		return {
			topBarHeight: 0,
			windowHeight: 0,
			back_k: 0, // 物理返回
			popupIsShow: false, // 打开设置的标识
			signOutShow: false, // 退出登录
			percent: 0
		};
	},
	onLoad() {
		// 拦截物理返回键
		this.goBackBox();
	},
	onUnload() {
		uni.$off('goBack_k', this.goBackBox2);
	},
	onHide() {
		if (this.$refs.SetUpPopup) {
			this.$refs.SetUpPopup.close();
		}
		if (this.$refs.signOutBox) {
			this.$refs.signOutBox.close();
		}
		this.popupIsShow = false;
	},
	created() {
		const that = this;
		uni.getSystemInfo({
			success(res) {
				that.topBarHeight = res.statusBarHeight;
				that.windowHeight = res.windowHeight;
			}
		});
	},
	components: { setUpPages, signOut, bottomPages, arprogress },
	computed: {
		topBar() {
			let num = 0;
			num = this.topBarHeight + 44; // 44是顶部高度
			return num + 'px';
		},
		i18n() {
			return this.$t('cartoon');
		}
	},
	methods: {
		goBackBox() {
			// const that = this;
			uni.$on('goBack_k', this.goBackBox2);
		},
		goBackBox2(data) {
			this.back_k < 0 ? (this.back_k = 0) : (this.back_k = this.back_k);
			if (data) {
				if (this.popupIsShow) {
					this.$refs.SetUpPopup.close();
					this.popupIsShow = false;
				} else if (this.signOutShow) {
					this.$refs.signOutBox.close();
					this.signOutShow = false;
				} else {
					let main = plus.android.runtimeMainActivity();
					if (this.back_k > 0) {
						this.back_k = 0;
						return main.moveTaskToBack(false);
						// return plus.runtime.quit();
					} else {
						const pages = getCurrentPages();
						if (pages.length === 1) {
							this.back_k++;
							setTimeout(() => {
								this.back_k--;
							}, 1000);
							// #ifdef APP-PLUS
							uni.redirectTo({
								url: '/pages/home/index'
							});
							// #endif
							// #ifdef H5
							this.$Router.push('/pages/home/index');
							// #endif
							// uni.showToast({
							// 	title: '再按一次退出应用',
							// 	icon: 'none',
							// 	duration: 1000
							// });
						}
					}
				}
			}
		},
		// 下载本地pdf
		downLoad() {
			this.$refs.ImgPopup.open();
			const that = this;
			var downloadTask = uni.downloadFile({
				// url: 'https://ctwcms.oss-ap-southeast-1.aliyuncs.com/web/shipment/2021/12/24/1474282627427594240.jpg',
				url: 'https://ctwcms.oss-ap-southeast-1.aliyuncs.com/web/ctw_markets/daochu.pdf',
				success: function(res) {
					console.log(res);
					that.$refs.ImgPopup.close();
					setTimeout(() => {
						that.percent = 0;
					}, 500);
					var filePath = res.tempFilePath;
					uni.openDocument({
						filePath: filePath,
						showMenu: false,
						success: function(res2) {
						}
					});
				},
				fail: function(err) {
					console.log(err);
					that.$refs.ImgPopup.close();
					setTimeout(() => {
						that.percent = 0;
					}, 500);
					uni.showToast({
						title: '下载失败',
						icon: 'none',
						position: 'top'
					});
				}
			});
			downloadTask.onProgressUpdate(res => {
				// console.log((res.totalBytesWritten / (1024 * 1024)).toFixed(0));
				// console.log((res.totalBytesExpectedToWrite / (1024 * 1024)).toFixed(0));
				that.percent = res.progress;
			});
			// uni.openDocument({
			// 	filePath: 'https://ctwcargo.yuntai-test.com/uploads/app/cartoon.pdf',
			// 	showMenu: false,
			// 	success: function(res) {
			// 		console.log(res);
			// 	},
			// 	fail: function(err) {
			// 		console.log(err);
			// 	}
			// });
		},
		// 返回home
		goHome() {
			// #ifdef APP-PLUS
			uni.redirectTo({
				url: '/pages/home/index'
			});
			// #endif
			// #ifdef H5
			this.$Router.push('/pages/home/index');
			// #endif
		},
		// 打开设置
		showSetUp() {
			this.$refs.SetUpPopup.open('right');
			this.popupIsShow = true;
		},
		// 关闭设置
		closePopUp(val) {
			if (val) {
				this.$refs.SetUpPopup.close();
				this.popupIsShow = false;
			} else {
				this.$refs.SetUpPopup.close();
				this.popupIsShow = false;
				this.$refs.signOutBox.open();
				this.signOutShow = true;
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.titleImg {
	width: 60px;
	height: 30px;
	margin: 0 auto;
	border-radius: 50%;
	overflow: hidden;
}
.leftIconBox {
	width: 25px;
	height: 25px;
	background-color: #fff;
	border-radius: 50%;
	display: flex;
	justify-content: center;
	align-items: center;
	margin-left: 15px;
}
.topBox {
	position: fixed;
	top: 0;
	left: 0;
	z-index: 10;
	// 顶部部分
	.status_bar {
		height: var(--status-bar-height);
		background-color: #f1f1f1;
		width: 100%;
	}
}
.zhuti {
	.imageTopBox {
		width: 100%;
		height: 100px;
	}
	.tabTopBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 16px 10px;
		// height: 40px;
		// line-height: 40px;
		box-sizing: border-box;
		.tabTopBoxL {
			color: #CD001B;
			font-size: $uni-font-size-custom-e;
			font-weight: 700;
		}
		.tabTopBoxR {
			display: flex;
			// align-items: center;
			border-radius: 3px;
			overflow: hidden;
			.tabTopBoxRImg {
				background-color: #CD001B;
				padding: 0 6px;
			}
			.tabTopBoxRText {
				padding: 0 6px;
				background-color: #f5f5f5;
				font-size: $uni-font-size-custom-h;
				line-height: 8px;
				display: flex;
				justify-content: center;
				flex-direction: column;
			}
		}
	}
	.neironBox {
		padding: 0 100rpx;
		box-sizing: border-box;
		background-color: #eaf4fd;
		width: 100%;
		height: 800px;
		margin-bottom: 20px;
	}
}
</style>
